package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.datamation.swadeshi.model.FDaynPrdHed;
import com.datamation.swadeshi.model.fDaynPrdDet;

public class FDaynPrdHedDS {
	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	
	private String TAG="swadeshi";
	
	
	public FDaynPrdHedDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}
	
	
	/*
	 * insert code
	 */
	@SuppressWarnings("static-access")
	public int createOrUpdateNonPrdHed(ArrayList<FDaynPrdHed>  list) {
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
		
    for (FDaynPrdHed nonhed : list) {
    		ContentValues values = new ContentValues();
			
			values.put(dbHelper.NONPRDHED_REFNO, 	nonhed.getNONPRDHED_REFNO());
			values.put(dbHelper.NONPRDHED_TXNDAET, 	nonhed.getNONPRDHED_TXNDAET());
			values.put(dbHelper.NONPRDHED_DEALCODE, nonhed.getNONPRDHED_DEALCODE());
			values.put(dbHelper.NONPRDHED_REPCODE, 	nonhed.getNONPRDHED_REPCODE());
			values.put(dbHelper.NONPRDHED_REMARK, 	nonhed.getNONPRDHED_REMARK());
			values.put(dbHelper.NONPRDHED_COSTCODE, nonhed.getNONPRDHED_COSTCODE());
			values.put(dbHelper.NONPRDHED_ADDUSER, 	nonhed.getNONPRDHED_ADDUSER());
			values.put(dbHelper.NONPRDHED_ADDDATE, 	nonhed.getNONPRDHED_ADDDATE());
			values.put(dbHelper.NONPRDHED_ADDMACH, 	nonhed.getNONPRDHED_ADDMACH());
			values.put(dbHelper.NONPRDHED_TRANSBATCH,nonhed.getNONPRDHED_TRANSBATCH());
			values.put(dbHelper.NONPRDHED_IS_SYNCED, nonhed.getNONPRDHED_IS_SYNCED());
			values.put(dbHelper.NONPRDHED_ADDRESS, nonhed.getNONPRDHED_ADDRESS());
			
									
			count = (int) dB.insert(dbHelper.TABLE_NONPRDHED, null, values);

			}
		}catch (Exception e) {
		
			Log.v(TAG+" Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return count;
				
	}
	public ArrayList<FDaynPrdHed> getAllnonprdHedDetails() {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<FDaynPrdHed> list = new ArrayList<FDaynPrdHed>();
		
		String selectQuery = "select * from "+dbHelper.TABLE_NONPRDHED +"";
		
		Cursor cursor = dB.rawQuery(selectQuery, null);
		while(cursor.moveToNext()){
			
			FDaynPrdHed fnonset=new FDaynPrdHed();
		
			fnonset.setNONPRDHED_REFNO(cursor.getString(cursor.getColumnIndex(dbHelper.NONPRDHED_REFNO)));
			fnonset.setNONPRDHED_ADDDATE(cursor.getString(cursor.getColumnIndex(dbHelper.NONPRDHED_ADDDATE)));
	
							
			list.add(fnonset);
			
		}
		
		return list;
	}
	@SuppressWarnings("static-access")
	public int undoOrdHedByID(String RefNo){
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		try{
			
			cursor = dB.rawQuery("SELECT * FROM " + dbHelper.TABLE_NONPRDHED +" WHERE "+dbHelper.NONPRDHED_REFNO+"='"+RefNo+"'", null);
			count =cursor.getCount();
			if(count>0)
			{
				int success = dB.delete(dbHelper.TABLE_NONPRDHED, dbHelper.NONPRDHED_REFNO+"='"+RefNo+"'", null);
				
			}
		}catch (Exception e){

			
			
		}finally{  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		
		return count;
		
	}
}
